package com.eightylegs.customer.default80legs;

import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;

import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.html.parser.ParserDelegator;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTML.Attribute;

public class DefaultParseLinks {
	//Used prior to 09/02/2009
	//private static final String REGEX_PARSE_LINKS = "((?i)((href)|(HREF))\\s*=\\s*[\"'](.+?)[\"'])|((?i)((src)|(SRC))\\s*=\\s*[\"'](.+?)[\"'])|((?i)<((meta)|(META))\\s*((http)|(HTTP))-((equiv)|(EQUIV))\\s*=\\s*[\"']\\s*((refresh)|(REFRESH))\\s*[\"'].*>)";
	//private static final String REGEX_PARSE_LINKS = "((?i)((href)|(HREF))\\s*=\\s*(.+?)([\"'>]))|((?i)((src)|(SRC))\\s*=\\s*(.+?)([\"'>]))|((?i)<((meta)|(META))\\s*((http)|(HTTP))-((equiv)|(EQUIV))\\s*=\\s*[\"']\\s*((refresh)|(REFRESH))\\s*[\"'].*>)";
	
	public static Collection<String> parseLinks ( String pageContents, URL url, boolean allowQueryStrings ) throws IOException, Exception {
		HashSet<String> outlinkSet = new HashSet<String>();
		
		outlinkSet = HTMLEditorKitParseLinks(pageContents, url, allowQueryStrings);
		
		return outlinkSet;
	}
	
	
	public static HashSet<String> HTMLEditorKitParseLinks ( String documentContent, URL crawlUrl, boolean allowQueryStrings ) throws IOException, Exception {
		final HashSet<String> outlinkSetBefore = new HashSet<String>();
		final StringBuilder baseUrlStringSB = new StringBuilder();
		HashSet<String> outlinkSetAfter = new HashSet<String>();

		try {
			ParserDelegator parserDelegator = new ParserDelegator();
		    ParserCallback parserCallback = new ParserCallback() {
			    public void handleText(final char[] data, final int pos) {
			    }
	
			    public void handleStartTag(Tag tag, MutableAttributeSet attribute, int pos) {
			    	String address = "";
			    	if (tag == Tag.A ) {
			    		address = (String) attribute.getAttribute(Attribute.HREF);
			        }
			    	outlinkSetBefore.add(address);
			    }
	
			    public void handleEndTag(Tag t, final int pos) {
			    }
	
			    public void handleSimpleTag(Tag tag, MutableAttributeSet attribute, final int pos) {
			    	String address = "";
			    	if (tag == Tag.LINK) {
			    		address = (String) attribute.getAttribute(Attribute.HREF);
			    	}
			    	else if (tag == Tag.IMG) {
			    		address = (String) attribute.getAttribute(Attribute.SRC);
			    	}
			    	else if (tag == Tag.BASE) {
			    		if (baseUrlStringSB.length() <= 0) {
			    			baseUrlStringSB.append((String) attribute.getAttribute(Attribute.HREF));
			    		}
			    	}
			    	else if (tag != null && attribute != null && attribute.getAttribute(Attribute.SRC) != null){
			    		try {
			    			address = (String) attribute.getAttribute(Attribute.SRC);
			    		}
			    		catch (Exception e) {
			    			//e.printStackTrace();
			    		}
			    	}
			    	
			    	if (address != null && address.equals("") == false) {
			    		outlinkSetBefore.add(address);
			    	}
			    }
	
			    public void handleComment(final char[] data, final int pos) {
			    }
	
			    public void handleError(final java.lang.String errMsg, final int pos) {
			    }
		    };
		    
		    //InputStream is = new ByteArrayInputStream(documentContent, 0, documentContent.length);
			//Reader rd = new InputStreamReader(is);
		    StringReader rd = new StringReader(documentContent);
		    parserDelegator.parse(rd, parserCallback, true);
		    
		    URL baseUrl = null;
		    try {
		    	baseUrl = new URL(baseUrlStringSB.toString());
		    }
		    catch (Exception e) {	
		    }
		    
		    if (baseUrl == null) {
		    	baseUrl = crawlUrl;
		    }
		    
		    for (String tempLink : outlinkSetBefore) {
		    	try {
			    	if (tempLink != null && !tempLink.equals("")) {
			    		tempLink =  URLFunctions.preProcessParsedLink(tempLink, crawlUrl, baseUrl, allowQueryStrings);
						if (!tempLink.equals("")) {	
							outlinkSetAfter.add(tempLink);
						}	
			    	}
		    	}
		    	catch (Exception e) {
		    		//Ignore links that throw exception
		    	}
		    }
		}
		finally {		
		}
		
		return outlinkSetAfter;
	}

}
